$(function(){
	
	var aid;
	var cid;
	
	//1.解析url,获取aid
	//http://10.25.41.3:8066/wechatvote/baoming.html?aid=1&cid=2&bid=2&did=2
	//截取?后面的内容
	var index = location.href.lastIndexOf("?");//字符串中，最后一个问号的位置
	var params = location.href.substr(index+1);//params='aid=1&cid=2&bid=2&did=2'
	
	//2.按照&分割
	var arr = params.split("&");
	//遍历数组
	for(var i=0;i<arr.length;i++){
		//if,为了找到aid
		if(arr[i].startsWith("aid")){
			var index = arr[i].lastIndexOf("=");//aid=1
			aid = arr[i].substr(index+1);
		}
		
		if(arr[i].startsWith("cid")){
			var index = arr[i].lastIndexOf("=");//cid=1
			cid = arr[i].substr(index+1);
		}
	}
	
	$.ajax({
		type:"get",
		url:"candidatesDetails?aid="+aid+"&cid="+cid,
		async:true,
		dataType:"json",
		success:function(data){
			
			$("#imgurl").attr("src","img/"+data.imgurl);
			$("#cname").html(data.cname);
			$("#cid").html(data.cid);
			$("#tickets").html(data.tickets);
			$("#hots").html(data.hots);
			$("#gifts").html(data.gifts);
			$("#cdeclaration").html(data.cdeclaration);
			
			for(var i=0;i<data.images.length;i++){
				//<img src="img/1.png"/>
				$("#images").append('<img src="img/'+data.images[i].imgurl+'"/>')
			}
			//把data放在一个存储对象中sessionStorage、cookie
			//JSON.parse() 字符串转json对象
			//JSON.stringify() json对象转字符串
			sessionStorage.candidate = JSON.stringify(data);
			
		}
	});
	
	//定义回到首页
	$("#gohome").click(function(){
		location.href = "index.html?aid="+aid;
	});
	
	//定义送礼事件
	$("#go_lw").click(function(){
		location.href = "lw.html?aid="+aid+"&cid="+cid;
	});
	
	//定义投票点击事件
	$("#btn_vote").click(function(){
		
		$.ajax({
			type:"post",
			url:"candidatesVote?aid="+aid+"&cid="+cid,
			dataType:"json",
			success:function(data){
				alert(data.result);
				if(data.result == '投票成功'){
					var t = parseInt($("#tickets").html());
					$("#tickets").html(t + 1);
					
					var c = JSON.parse(sessionStorage.candidate);
					c.tickets = parseInt(c.tickets)+1;
					var str = JSON.stringify(c);
					sessionStorage.candidate = str;
				}
			}
		});
	});
	
})
